package com.ruoyi.web.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.system.domain.BdmAgreements;

import com.ruoyi.system.service.IBdmAgreementsService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 协议对象 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-31
 */
@Api(tags = "后台-协议")
@RestController
@RequestMapping("/admin/agreements")
public class BdmAgreementsController extends BaseController {

    @Autowired
    private IBdmAgreementsService iBdmAgreementsService;

    /**
     * 查询协议
     */
    @GetMapping("/list")
    public TableDataInfo list(BdmAgreements bdmAgreements) {
        startPage();
        LambdaQueryWrapper<BdmAgreements> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(bdmAgreements.getTitle())){
            wrapper.like(BdmAgreements::getTitle, bdmAgreements.getTitle());
        }
        wrapper.orderByDesc(BdmAgreements::getCreateTime);
        List<BdmAgreements> list = iBdmAgreementsService.list(wrapper);
        return getDataTable(list);
    }


    /**
     * 获取协议
     */
    @GetMapping(value = "/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(iBdmAgreementsService.getById(id));
    }

    /**
     * 新增协议
     */
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody BdmAgreements bdmAgreements) {
        bdmAgreements.setCreateTime(LocalDateTime.now());
        bdmAgreements.setCreateBy(getUsername());
        return toAjax(iBdmAgreementsService.save(bdmAgreements));
    }

    /**
     * 修改协议
     */
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BdmAgreements bdmAgreements) {
        bdmAgreements.setUpdateTime(LocalDateTime.now());
        bdmAgreements.setUpdateBy(getUsername());
        return toAjax(iBdmAgreementsService.updateById(bdmAgreements));
    }

    /**
     * 删除协议
     */
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @PostMapping("/remove}")
    public AjaxResult remove(@RequestBody List<Long> ids) {
        return toAjax(iBdmAgreementsService.removeBatchByIds(ids));
    }
}
